package com.shanshi.activity.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shanshi.activity.R;
import com.shanshi.activity.request.EssayInfo;
import com.shanshi.activity.request.SendReply;
import com.shanshi.activity.tool.CommentDialog;
import com.shanshi.activity.tool.OkHttpTool;
import com.shanshi.activity.tool.TextCircleView;

import java.util.List;

import static com.shanshi.activity.R.drawable.praise;

/**
 * Created by cz on 2017/3/21.
 */

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.MyCommentViewHolder> {
    private List<EssayInfo.DataBean.CommentArrayBean> list;
    private Context context;
    private EssayInfo.DataBean.CommentArrayBean commentArrayBean;
    private Gson gson = new Gson();
    private EditText message;
    private String userId;
    private String articleId;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    Log.e("====", "提交回复成功");
                    replyAdapter.notifyDataSetChanged();
                    break;

                default:
                    break;
            }
        }

        ;
    };
    private ReplyAdapter replyAdapter;

    public CommentAdapter(List<EssayInfo.DataBean.CommentArrayBean> list, Context context, String userId, String articleId) {
        Log.e("====", "适配器" + list.size() + "=====" + list.toString());
        this.list = list;
        this.context = context;
        this.userId = userId;
        this.articleId = articleId;
    }


    @Override
    public MyCommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
        return new MyCommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyCommentViewHolder holder, int position) {
        commentArrayBean = list.get(position);
        holder.tv_name.setText(commentArrayBean.getRealName());
        holder.tv_time.setText(commentArrayBean.getCommentTime());
        holder.tv_comment.setText(commentArrayBean.getContent());
        holder.tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        holder.tv_praiseNum.setText(commentArrayBean.getPraiseCount() + "");
        if (commentArrayBean.isPraiseState()) {
            holder.img_comment_parsie.setImageResource(R.drawable.praise_no);
        } else {
            holder.img_comment_parsie.setImageResource(praise);
        }
        holder.img_comment_parsie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (commentArrayBean.isPraiseState()) {
                    holder.img_comment_parsie.setImageResource(praise);
                    String rebody = gson.toJson("false");
                    OkHttpTool.doDelete(OkHttpTool.BASE_URL + "users/" + userId + "/articles/{articleId}/praise", rebody);
                    holder.tv_praiseNum.setText(commentArrayBean.getPraiseCount() - 1);
                } else {
                    holder.img_comment_parsie.setImageResource(R.drawable.praise_no);
                    String rebody = gson.toJson("true");
                    OkHttpTool.doPost(OkHttpTool.BASE_URL + "users/" + userId + "/articles/" + articleId + "/praise", rebody, new OkHttpTool.setOkhttpListener() {
                        @Override
                        public void onSuccess(String s) {
                            handler.sendEmptyMessage(1);
                        }

                        @Override
                        public void onFail() {

                        }
                    });
                    holder.tv_praiseNum.setText((commentArrayBean.getPraiseCount()) + 1 + "");
                }
            }
        });
        holder.textCircleView.setText(commentArrayBean.getRealName().charAt(0) + "");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        };
        holder.rec_reply.setLayoutManager(linearLayoutManager);
        List<EssayInfo.DataBean.CommentArrayBean.Reply> replyList = commentArrayBean.getReply();
        replyAdapter = new ReplyAdapter(context, replyList, userId);
        holder.rec_reply.setAdapter(replyAdapter);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyCommentViewHolder extends RecyclerView.ViewHolder {
        ImageView img_comment_parsie;
        TextCircleView textCircleView;
        TextView tv_name, tv_time, tv_praiseNum, tv_comment;
        RecyclerView rec_reply;

        public MyCommentViewHolder(View itemView) {
            super(itemView);
            img_comment_parsie = (ImageView) itemView.findViewById(R.id.img_comment_parsie);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_praiseNum = (TextView) itemView.findViewById(R.id.tv_praiseNum);
            tv_comment = (TextView) itemView.findViewById(R.id.tv_comment);
            rec_reply = (RecyclerView) itemView.findViewById(R.id.rec_reply);
            textCircleView = (TextCircleView) itemView.findViewById(R.id.img_head);
        }
    }

    public void showDialog() {
        final CommentDialog commentDialog = new CommentDialog(context);
        message = commentDialog.getMessageEditText();
        commentDialog.setDialogclose(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commentDialog.dismiss();
            }
        });
        commentDialog.setOnSendMessage(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (message.getText() != null) {
                    SendReply reply = new SendReply();
                    reply.setId(commentArrayBean.getUserId());
                    reply.setType(1);
                    reply.setContent(message.getText().toString());
                    String rebody = gson.toJson(reply);
                    OkHttpTool.doPost(OkHttpTool.BASE_URL + "users/" + userId + "/commentOrReply", rebody, new OkHttpTool.setOkhttpListener() {
                        @Override
                        public void onSuccess(String s) {
                            handler.sendEmptyMessage(1);
                        }

                        @Override
                        public void onFail() {

                        }
                    });
                }
            }
        });
        commentDialog.show();
    }
}
